package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.EmployeeInfo;
import com.suyuan.entity.PigOut;
import com.suyuan.service.PigOutService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.PigOutVO;
import com.suyuan.web.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 生猪信息控制类
 */
@Controller
@RequestMapping("/admin/PigOut")
public class PigOutController  {

    @Resource(name = "pigOutService")
    private PigOutService pigOutService;

    private Map<String,Object> msg;



    /**
     * 跳转到添加生猪出库信息页面
     * @return
     */
    @RequestMapping("toAdd")
    public String toAddPage(){
        return "pigOut_add";
    }

    /**
     * 添加生猪出场信息
     * @param pigOut
     * @return
     */
    @RequestMapping("addPigOut")
    @ResponseBody
    public Object addPigOut(PigOut pigOut){
        int code = 0;
        try{
            pigOutService.addPigOut(pigOut);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }


    /**
     * 检查生猪出场前是否检疫
     * @param pid
     * @return
     */
    @RequestMapping("checkPig")
    @ResponseBody
    public Map<String,Object> checkPig(Integer pid){
        msg = new HashMap<String, Object>();
        Integer flag = pigOutService.checkPig(pid);
        msg.put("msg",flag);
        return msg;
    }


    /**
     * 根据条件查询生猪出库信息
     * @param beginTime
     * @param endTime
     * @param purGid
     * @return
     */
    @RequestMapping("findByCondition")
    public String list(Date beginTime, Date endTime, Integer purGid, Page page, ModelMap modelMap){
        PageInfo pageInfo = pigOutService.findPigout(beginTime,endTime,purGid,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("purGid",purGid);
        modelMap.addAttribute("page",page);
        return  "pigOut_list";
    }


    /**
     * 删除生猪出场信息
     * @param
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deletePigOutInfo(String ids){
        Integer code = pigOutService.deletePigOutInfo(ids);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }


    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deleteInfo(Integer id){
        return pigOutService.deletePigOut(id);
    }


    /**
     * 跳转编辑页面
     * @param gid
     * @return
     */
    @RequestMapping("toedit")
    public String toedit(Integer gid ,ModelMap modelMap){
        PigOut pigOut = pigOutService.findPigOutByID(gid);
        modelMap.addAttribute("pigOut",pigOut);
        return "pigOut_edit";
    }


    /**
     *  更新生猪出场信息
     * @param pigOut
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Integer updateInfo(PigOut pigOut){
        int code = 0;
        try{
            pigOutService.updateInfo(pigOut);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }


}
